Ext.define('jwawa.erp.cfg.searchPage.SearchPageModel', {
    extend: 'jwawa.erp.framework.JwawaModel',
    isRequestProcess: false,
//    store:{
//        className:'Ext.data.Store',
//        cfg:{
//            fields: [
//                {name: 'id',  type: 'int'},
//                {name: 'page_name',  type: 'string'},
//                'page_code'
//            ],
//            proxy:{
//                type: 'jwawaProxy',
//                url : '/search-page/find-search-page-list',
//                reader: {
//                    type: 'json',
//                    root: 'data'
//                }
//            },
//            pageSize: DEFAULT_PAGE_SIZE,
//            autoLoad: false
//        }
//    },

    constructor: function(config){
        var me = this;

        Ext.apply(me, config);
        me.callParent(arguments);

//		me.getStore().addListener('load', function(){
//			me.isRequestProcess = false;
//		})
    },

//    search: function(searchData, opt){
//        var me = this;
//
//		if(me.isRequestProcess){
//			return;
//		} else {
//			me.isRequestProcess = true;
//		}
//
////            me.getStore().load({params:{ss: 'xx'}, page:2, start: 1, limit: 2});
//        me.getStore().getProxy().extraParams = searchData;
//        me.getStore().loadPage(1);
//    },

    getSearchPageAndItem: function(pageId, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        Jwawa.framework.JwawaConnection.request({
            url      : '/search-page/get-page-and-item-by-id',
            async    : true,
            jsonData: {id: pageId},
            success : function(response) {
                var data = response["data"];
                var searchPage = data['searchPage'];
                var searchItemList = data['searchItemList'];

                optional['success'](searchPage, searchItemList);
            },
            failure:function(response){
                optional['failure'](response);
            },
            callback: function(options, success, response){
                optional['callback'](options);
            }
        });
    },

    deleteSearchPage: function(pageIds, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        if(Ext.isArray(pageIds) && pageIds.length > 0){
            Jwawa.framework.JwawaConnection.request({
                url      : '/search-page/del-page-by-ids',
                async    : true,
                jsonData: {ids: pageIds},
                success : function(response) {
//                    Ext.each(pageIds, function(pageId){
//                        var searchPage = me.getStore().getById(pageId);
//                        me.getStore().remove(searchPage);
//                    });

                    optional['success'](pageIds, response);
                },
                failure:function(response){
                    optional['failure'](response);
                },
                callback: function(options, success, response){
                    optional['callback'](response);
                }
            });
        }
    },

    saveSearchPage: function(searchPage, opt){
        "use strict";
        var me = this,
            optional = me.buildOpt(opt);

        Jwawa.framework.JwawaConnection.request({
            url      : '/search-page/save-search-page',
            async    : true,
            jsonData: searchPage,
            success : function(response) {
                var searchPage = response['data'];
//                    searchPageRecord = me.getStore().getById(searchPage['id']);
//                if(searchPageRecord){
//                    searchPageRecord.set('page_name', searchPage['page_name']);
//                    searchPageRecord.set('page_code', searchPage['page_code']);
//                    searchPageRecord.commit();
//                }
                optional['success'](searchPage, response);
            },
            failure:function(response){
                optional['failure'](response);
            },
            callback: function(options, success, response){
                optional['callback'](options);
            }
        });
    }
});